package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.enums.OrderStatus;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.*;
import com.tencreat.service.mall.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 自提订单Controller
 *
 * @author tencreat
 * @date 2022-01-25
 */
@Api(tags="自提订单api")
//@RestController
@RequestMapping("/system/selfmentionOrder")
public class AdminMallSelfmentionOrderController extends BaseController {
    //@Autowired
    private IMallSelfmentionOrderService mallSelfmentionOrderService;

    //@Autowired
    private IMallOrderGoodsService mallOrderGoodsService;

    //@Autowired
    private IMallMessageInformService mallMessageInformService;
    //@Autowired
    private IMallOrderService mallOrderService;

    /**
     * 查询自提订单列表
     */
    @ApiOperation(value="查询自提订单列表")
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallSelfmentionOrder>> list(@RequestBody  MallSelfmentionOrder mallSelfmentionOrder) {
        QueryWrapper<MallSelfmentionOrder> wrapper = Wrappers.query(mallSelfmentionOrder);
        Page<MallSelfmentionOrder> page =  new Page<>(mallSelfmentionOrder.getPageNum(), mallSelfmentionOrder.getPageSize());
        return HttpResult.ok(mallSelfmentionOrderService.page(page, wrapper));
    }

    /**
     * 导出自提订单列表
     */
    @ApiOperation(value="导出自提订单列表")
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "自提订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallSelfmentionOrder mallSelfmentionOrder) {
        List<MallSelfmentionOrder> list = mallSelfmentionOrderService.selectMallSelfmentionOrderList(mallSelfmentionOrder);
        ExcelUtil<MallSelfmentionOrder> util = new ExcelUtil<MallSelfmentionOrder>(MallSelfmentionOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 获取自提订单详细信息
     */
    @ApiOperation(value="获取自提订单详细信息")
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallSelfmentionOrder> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallSelfmentionOrderService.selectMallSelfmentionOrderById(id));
    }

    /**
     * 新增自提订单
     */
    @ApiOperation(value="新增自提订单")
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "自提订单", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallSelfmentionOrder mallSelfmentionOrder) {
        return to(mallSelfmentionOrderService.insertMallSelfmentionOrder(mallSelfmentionOrder));
    }

    /**
     * 修改自提订单
     */
    @ApiOperation(value="修改自提订单")
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "自提订单", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallSelfmentionOrder mallSelfmentionOrder) {
        return to(mallSelfmentionOrderService.updateMallSelfmentionOrder(mallSelfmentionOrder));
    }

    /**
     * 删除自提订单
     */
    @ApiOperation(value="删除自提订单")
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "自提订单", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallSelfmentionOrderService.deleteMallSelfmentionOrderByIds(ids));
    }

    /**
     * 核销自提订单
     */
    @ApiOperation(value="核销")
    @Log(title = "订单管理>自提订单", businessType = BusinessType.UPDATE)
    @PostMapping("/check")
    public HttpResult<Boolean> check(@RequestBody  MallSelfmentionOrder mallSelfmentionOrder) {
        MallSelfmentionOrder selfmentionOrder = mallSelfmentionOrderService.getById(mallSelfmentionOrder.getId());
        if(!selfmentionOrder.getCheckNumber().equals(mallSelfmentionOrder.getCheckNumber())){
            return HttpResult.error("核销码有误");
        }
        //修改订单状态
        mallOrderGoodsService.updateOrderTypeByGoodsNorms(selfmentionOrder.getOrderSn(),selfmentionOrder.getGoodsId(),selfmentionOrder.getNormsId(), OrderStatus.DAI_CENTXT.getValue(), LocalDateTime.now());
        MallOrderGoods mallOrderGoods = mallOrderGoodsService.lambdaQuery().eq(MallOrderGoods::getOrderSn, selfmentionOrder.getOrderSn()).eq(MallOrderGoods::getGoodsId, selfmentionOrder.getGoodsId()).eq(MallOrderGoods::getNormsId, selfmentionOrder.getNormsId()).one();
        MallOrder mallOrder = mallOrderService.lambdaQuery().eq(MallOrder::getOrderSn, selfmentionOrder.getOrderSn()).one();
        mallMessageInformService.addOrderMessage("您的商品取货成功",mallOrderGoods.getId(), mallOrder.getUuid());

        selfmentionOrder.setTake(1L);
        boolean update = mallSelfmentionOrderService.updateById(selfmentionOrder);
        if (update) {
            // 处理分佣和货款
            mallOrderService.handleData(mallOrderGoods);
        }

        return to(update);
    }

}
